<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04_createRef使用</title>
</head>

<body>
    <div id="app"></div>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class MyComponent extends React.Component{
            // 使用 createRef 创建 ref,此时input1是一个存放被ref表示的元素DOM的容器，这个容器只能装一个元素
            input1 = React.createRef()
            input2 = React.createRef()

            // 显示input1的数据
            showData1 = ()=>{
                // 使用 createRef 的形式后获取到的元素DOM必须从 current 中获取DOM数据
                alert(this.input1.current.value);
            }

            showData2 = ()=>{
                alert(this.input2.current.value)
            }

            render(){
                return (
                    <div>
                        <input ref={this.input1} placeholder="点击按钮弹出信息"/>
                        <button onClick={this.showData1}>显示左侧信息</button>
                        <input ref={this.input2} onBlur={this.showData2}  placeholder="离焦点触发"/>
                    </div>
                )
            }
        }

        ReactDOM.render(<MyComponent/>,document.getElementById("app"))
    </script>
</body>

</html>